<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 部门表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">

                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>

                <el-input v-model="query.dname" placeholder="名称搜" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>

                <el-button type="primary" @click="showAddDialog">添加部门</el-button>
            </div>

            <el-table
                :data="tableData"
                border
                class="table"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column type="index" label="序号" width="70" align="center"></el-table-column>
                <el-table-column prop="name" label="部门名称"></el-table-column>
                <el-table-column prop="sn" label="部门别名"></el-table-column>
                <el-table-column label="状态">
                    <template #default="scope" >
                        <span :style="{color: scope.row.state==0?'green':'red'}">
                            {{ scope.row.state==0?'启用':'禁用' }}</span>
                        <!--
                        :active-value="0"   开关打开的值
                        :inactive-value="1" 开关关闭的值
                        -->
                            <el-switch
                                    v-model="scope.row.state"
                                    class="mb-2"
                                    :active-value="0"
                                    :inactive-value="1"
                                    @change="changeState(scope.row)"
                            />
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    :page-sizes="[3, 5, 8, 10]"
                    layout="total, sizes, prev, pager, next"
                    v-model:current-page="query.current"
                    v-model:page-size="query.size"
                    :total="total"
                    @current-change="getData"
                    @size-change="getData"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑部门信息" v-model="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="别名">
                    <el-input v-model="form.sn"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch
                            v-model="form.state"
                            class="mb-2"
                            active-text="启用"
                            inactive-text="禁用"
                            :active-value="0"
                            :inactive-value="1"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "Department",
    data() {
        return {
            query: {
                dName: "",
                current: 1,  //当前页
                size: 10   //每页显示的记录数
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            total: 0,  //总记录数
            form: {
                name: '',
                sn: '',
                state: 0,
            },
        };
    },
    mounted() {
        this.getData();
    },
    methods: {
        changeState(row){
            this.$axios({
                method: 'post',
                url: '/department',
                data: {
                    id: row.id,
                    state: row.state
                }
            }).then(res=>{
                if(res.data.code!=200){
                    row.state = (row.state==0?1:0);
                    this.$message.error("更新状态失败");
                }
            }).catch(error=>{
                this.$message.error(error.message);
            })
        },
        //显示添加对话框
        showAddDialog(){
            this.form = {
                name: '',
                sn: '',
                state: 0,
            }
            this.editVisible = true
        },
        // 从服务端获取分页数据
        getData() {
            this.$axios({
                method:'post',
                url:'/department/page',
                data: this.query
            }).then(res=>{
                console.log(res);
                if (res.data.code==200){
                    this.tableData = res.data.data.records;
                    this.total = res.data.data.total;
                }
            }).catch(error=>{
                // this.$message.error(error.message);
            })

        },
        // 触发搜索按钮
        handleSearch() {
            this.query.current = 1
            this.getData();
        },
        // 删除操作
        handleDelete(row) {
            // 二次确认删除
            this.$confirm("确定要删除吗？", "提示", {
                type: "warning"
            })
            .then(() => {
                //确定删除之后发送删除请求
                this.$axios({
                    method:'delete',
                    url:'/department/'+row.id
                }).then(res=>{
                    if(res.data.code==200){
                        this.$message.success("删除成功");
                        this.getData();
                    }else{
                        this.$message.error("删除失败，请重试");
                    }
                }).catch(error=>{
                    this.$message.error(error.message);
                })
            })
            .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            //将data中存储的选中id数组置为空
            this.multipleSelection = [];
            //遍历选中行数组 拿到id放入选中id的数组
            val.forEach(department => {
                this.multipleSelection.push(department.id);
            });
            console.log(this.multipleSelection)
        },
        //批量删除的方法
        delAllSelection() {

            //判断有没有选中项
            if(this.multipleSelection.length==0){
                this.$message.error("没有任何选中项，无法删除");
                return;
            }

            this.$confirm("确定要删除这几项吗？", "提示", {
                type: "warning"
            })
            .then(() => {

                this.$axios({
                    method:'post',
                    url:'/department/batch',
                    data: this.multipleSelection
                }).then(res=>{
                    if(res.data.code==200){
                        this.$message.success("删除成功");
                        this.getData();
                    }else{
                        this.$message.error("删除失败，请重试");
                    }
                }).catch(error=>{
                    this.$message.error(error.message);
                })
            })
            .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.form = Object.assign({},row);
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            //发送添加或修改请求
            this.$axios({
                method:'post',
                url: '/department',
                data: this.form
            }).then(res=>{
                if(res.data.code == 200){
                    this.getData();
                    this.editVisible = false;
                    this.$message.success(`操作成功`);
                }else{
                    this.$message.error(`操作失败，请重试`);
                }
            }).catch(error=>{
                this.$message.error(error.message);
            })
           
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
